// pages/exam/detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isMult:true,
    fenshu:10,
    xuhao:1,
    total:10,
    minutes:0,
    endDate:'2020-04-06 07:30',
    title:'2020年3月6日，习近平总书记在决战决胜脱贫攻坚座谈会上强调，这次会议主要任务是，分析当前形势，客服____，凝心聚力____，确保____，确保____。',
    

    list:[],
    cur_index: 0,
    seconds: 0,
    user_exam_id: 0,
    id: 0,
    curquestion: {},
    haveMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    this.data.id = id;
    this.getExam();
  },
  getExam() {
    app.http.$ajax({
      url: 'v1.exam/getQuestion',
      data: {
        user_plan_id: this.data.id
      }
    }, {
        success: (res) => {
          this.setData({
            list: res.data.questions,
            cur_index: res.data.cur_index,
            seconds: res.data.time,
            user_exam_id: res.data.user_exam_id
          });

          this.getCurQuestion(res.data.cur_index);
        }
      });
  },
  getCurQuestion(cur_index) {
    let question = this.data.list[cur_index];
    
    if (question.type == 1) {
      question.type_name = '单选题'
    } else if (question.type == 2) {
      question.type_name = '多选题'
    } else {
      question.type_name = '判断题'
    }

    //判断当前题目状态
    question.selectdata.forEach((item) => {
      if (item.is_answer != 0) {
        //已经提交过答案
        if (question.is_answer.cur_answer == item.key) {
          item.isSeled = true
        }
      }
    });
    
    this.setData({
      cur_index: cur_index,
      curquestion: question
    });
  },
  answer() {
    let that = this;
    let select = [];
    //如果已经做过就不能修改答案
    if (this.data.curquestion.is_answer != 0) {
      //直接进入下一题
      if (this.data.cur_index < this.data.list.length - 1) {
        this.getCurQuestion(++this.data.cur_index);
      }
      return;
    }
    this.data.curquestion.selectdata.forEach((item) => {
      if (item.isSeled !== undefined && item.isSeled === true) {
        select.push(item.key);
      }
    })
    if(select.length <= 0) {
      return app.error('请选择答案');
    }
    select.sort();
    let post = {
      user_exam_id: this.data.user_exam_id,
      timu_info: this.data.curquestion.timu_info,
      user_answer: select
    }
    console.log(post);
    app.http.$ajax({
      url: 'v1.exam/answer',
      data: post,
      headers: {
        'Content-Type': 'application/json'
      },
    }, {
        success: (res) => {
          that.setData({
            ['curquestion.is_answer']: res.data
          });
          //直接进入下一题
          if (that.data.cur_index < that.data.list.length - 1) {
            that.getCurQuestion(++that.data.cur_index);
          }
        }
      });
  },
  submit() {
    app.http.$ajax({
      url: 'v1.exam/score',
      data: {
        id: this.data.user_exam_id
      }
    }, {
        success: (res) => {
          console.log(res);
        }
      });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  selThis:function(e){
    let _self = this;
    let key = e.currentTarget.dataset.key;
    let options = _self.data.curquestion.selectdata;
    //判断题目类型
    if (_self.data.curquestion.is_answer != 0) {
      return;
    }
    for(let i = 0; i < options.length; i++){
      if (key == options[i].key) {
        options[i].isSeled = !options[i].isSeled;
      } else {
        if (_self.data.curquestion.type != 2) {
          //非多选只能选一个
          options[i].isSeled = false
        }
      }
    }
    _self.setData({
      ['curquestion.selectdata'] : options
    });

  },
  goPrev:function(){
    let cur_index = this.data.cur_index;
    if (cur_index <= 0) {
      return
    } else {
      let cur_index = this.data.cur_index - 1;
      this.getCurQuestion(cur_index);
    }
  },
  goNext:function(){
    this.answer();
  }
})